import { Component, Input, ViewChild } from '@angular/core';
import { TableWidthConfig, TreeComponent } from 'ng-devui';
import { CustomerService } from 'src/app/@core/services/customer/customer.service';
import { SupplierService } from 'src/app/@core/services/supplier/supplier.service';
@Component({
  selector: 'app-select-supplier',
  templateUrl: './select-supplier.component.html',
  styleUrls: ['./select-supplier.component.scss']
})
export class SelectSupplierComponent {
  @Input() data: any;
  isSelected = false;
  displayTableData: any[] = [];
  displayTreeData: any[];
  treeData: any;
  tableheight = '230px';
  selectData: any;
  searchName: ''
  pager = {
    total: 20,
    pageIndex: 1,
    pageSize: 20
  };
  pageSizeOptions = [10, 20, 30, 50, 100];
  dataTableOptions = {
    columns: [
      {
        field: 'radio',
        header: '选择',
        fieldType: 'radio'
      },
      {
        field: 'code',
        header: '供应商编码',
        fieldType: 'text',
      },
      {
        field: 'name',
        header: '供应商名称',
        fieldType: 'text',
      },
      {
        field: 'name',
        header: '供应商简称',
        fieldType: 'text',
      },
      {
        field: 'name',
        header: '英文名称',
        fieldType: 'text',
      },
    ]
  };
  tableWidthConfig: TableWidthConfig[] = [
    {
      field: 'radio',
      width: '50px'
    },
    {
      field: 'code',
      width: '100px'
    },
    {
      field: 'name',
      width: '100px'
    },
    {
      field: 'name',
      width: '100px'
    },
    {
      field: 'name',
      width: '100px'
    }
  ]
  filter={
    SupplyClassId:null,
    PageSize:20,
    PageNumber:1,
    SearchTerm:null,
    SearchStatus:null
  }
  @ViewChild('basicTree', { static: true }) basicTree!: TreeComponent;
  supplierTypeId: any;
  tabledata: any;
  searchValue: any;
  filteredTreeNodes: any[];
  constructor(private SupplierService: SupplierService) { }
  ngOnInit(): void {
    this.SupplierService.getSupplierClass().subscribe((res:any)=>{
      this.treeData = res
      this.displayTreeData = [{ supplyClassName: '全部', supplyClassId: null }, ...this.treeData]
    })
    this.SupplierService.getSupplier(this.filter).subscribe((res:any)=>{
      this.tabledata = res.items
    })
  }
  afterTreeInit($event: any) {
    if ($event && Object.keys($event).length > 0) {
      if ($event[1].data.title=='全部') {
        $event[1].data.isActive = true
      }
    }
  }//树形菜单初始化
  nodeSelected($event:any){
    this.supplierTypeId = $event.data.originItem.supplyClassId;
    this.filter.SupplyClassId = this.supplierTypeId
    this.search()
  }
  searchClass($event: any) {
    this.searchValue = $event.target.value
    if(this.searchValue === '' || this.searchValue === null){
      this.displayTreeData = [{ supplyClassName: '全部', supplyClassId: null }, ...this.treeData]
    }else{
      this.filteredTreeNodes = this.filterCategories(this.treeData, this.searchValue);
      this.displayTreeData = this.filteredTreeNodes;
      this.displayTreeData = this.displayTreeData.map(this.setOpenTrue);
    }
  }
  filterCategories(categories: any[], keyword: string): any[] {
    const result: any[] = [];
    for (const category of categories) {
      if (category.supplyClassName && category.supplyClassName.includes(keyword)) {
        result.push(category);
      }
      if (category.subSupplyClasses?.length > 0) {
        const filteredSubCategories = this.filterCategories(category.subSupplyClasses, keyword);
        if (filteredSubCategories.length > 0) {
          result.push(...filteredSubCategories);
        }
      }
    }
    return result;
  }
  setOpenTrue = (node: any) => {
    // 为当前节点添加 open: true 属性
    node.open = true;
    // 如果当前节点有子节点，递归处理子节点
    if (Array.isArray(node.subSupplyClasses)) {
      node.subSupplyClasses = node.subSupplyClasses.map(this.setOpenTrue);
    }
    return node;
  }
  radio($event: any, rowItem: any) {
    if ($event.target.checked) {
      this.isSelected = true;
      this.selectData = rowItem
    }
  }
  search(){
    this.SupplierService.getSupplier(this.filter).subscribe((res:any)=>{
      this.tabledata = res.items
    })
  }
  reset() {
    this.filter = {
      SupplyClassId:null,
      PageSize:20,
      PageNumber:1,
      SearchTerm:null,
      SearchStatus:null
    }
    this.search()
  }//重置
  pageIndexChange() {
    this.search()
  }
  pageSizeChange() {
    this.search()
  }
}
